
var getUmpireList = function(callback){
	$.get('/s/umpires/list', callback);
};



var refreshGrid = function(){
	getUmpireList(function(data){
		var umpiresList = (data);
		var grid = getGrid();
		var ds = new kendo.data.DataSource({data:umpiresList})
		grid.setDataSource(ds);
		grid.refresh();				
	})
};


var onEditUmpire = function(e){
	e.preventDefault();	
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    window.location.href = "/s/umpires/edit?key="+ dataItem.key;    
}


var onUpdateUmpireFilters = function(e){

	var gridDs = $('#grid').data('kendoGrid').dataSource;
	
	umpireFilters = [];
	
	//filtering by location
	var locationValue = $('#filter_umpire_address').val();
	if(locationValue && locationValue.hasOwnProperty('length') && locationValue.length > 0){
		umpireFilters.push({field:'address', operator: 'contains', value:locationValue});		
	}
	
	//filtering by umpiretype
	var umpireTypeValue = $('#filter_umpire_type').val();
	if(umpireTypeValue){
		if(!(umpireTypeValue === 'showall')){
			umpireFilters.push({field:'umpireType', operator: 'eq', value:umpireTypeValue});
		}										
	}
	
	if(umpireFilters.length > 0){
		gridDs.filter(umpireFilters);
	}
	else{
		gridDs.filter({});
	}
}


$(function(){
	
	var umpiresViewModel = {};
	
	getUmpireList(function(data){		
		
		var umpiresList = (data);
		
		//setup grid for data
		var grid = $("#grid").kendoGrid({
	        dataSource: umpiresList,
	        sortable: true,
	        resizable: true,
	        toolbar: [{template: kendo.template($('#toolbar-umpire').html())}],
	        filterable: true,
	        selectable: "multiple",
	        pageable: {
	            refresh: true,
	            pageSizes: true
	        },
	        columns: [ {
	                field: "name",
	                width: 110,
	                title: "Name"
	            } , {
	                field: "nric",
	                width: 110,
	                title: "NRIC"
	            } , {
	                width: 150,
	                field: "email",
	                title: "Email"
	            } , {          
	            	width: 110,
	                field: "mobile",
	                title: "Mobile No"
	            } , {	            	
	                field: "umpireType",
	                title: "Type"
	            } , {                
	                field: "address"
	            } , {
	            	command : {name : 'Edit', click : onEditUmpire}
	            }
	        ]
	    });
		
		//setup filter dropdownlist
		$('#filter_umpire_type').kendoDropDownList({
			dataTextField:'text',
			dataValueField:'value',
			dataSource : [
			    {text:'Show All', value:'showall'},
			    {text:'International', value:'international'},
				{text:'National', value:'national'}
			],
			change: onUpdateUmpireFilters/*function() {
				var filterValue = this.value();
				if(filterValue){
					if(filterValue === 'showall'){
						grid.data('kendoGrid').dataSource.filter({});
					}else{
						grid.data('kendoGrid').dataSource.filter({field:'umpireType', operator: 'eq', value:filterValue});
					}										
				}
				else{
					grid.data('kendoGrid').dataSource.filter({});
				}
			}*/
		});
		
		//filtering by location
		$('#filter_umpire_address').keyup(/*function(e){
			var filterValue = $(this).val();
			if(filterValue && filterValue.hasOwnProperty('length') && filterValue.length > 0){
				grid.data('kendoGrid').dataSource.filter({field:'address', operator: 'contains', value:filterValue});
			}
			else{
				grid.data('kendoGrid').dataSource.filter({});
			}}*/
				
			onUpdateUmpireFilters
		);
		
		//setup button for toolbar
		$('#btn-newUmpire').click(function(e){
			window.location.href = '/s/umpires/new';
		});
		
		$('#btn-deleteSelected').click(function(e){			
			
			var grid = $('#grid').data('kendoGrid');
			var selectedRows = grid.select();
			if(selectedRows){
								
				var csrftoken = getCookie('csrftoken');
				
				var keys = $.map(selectedRows, function(k,i){
					var data =grid.dataItem(k);
					return data.key;
				});
				
				
				$.ajax({url:'/s/umpires/delete', 
						data : {keys: keys.join(",")},
						type: 'POST',
						beforeSend: function(request){
							request.setRequestHeader("X-CSRFToken", csrftoken);				         
						},
						success: function(data){
							refreshGrid();							
						}
				});		
				
			}
			
		});
		
		$('#btn-exportCsv').click(function(e){
			
			var addrFilter = $('#filter_umpire_address').val();
			var umpireTypeFilter = $('#filter_umpire_type').val();
			
			var queryString = '?address=' + addrFilter + '&umpireType=' + umpireTypeFilter;
			
			window.location.href = "/umpires/export/"+ queryString;
		});
		
	});
	
});